const { sequelize } = require('./config/db');

async function addApiConfigField() {
  try {
    await sequelize.authenticate();
    console.log('数据库连接成功');
    
    // 检查字段是否已存在
    const [columns] = await sequelize.query('DESCRIBE api_keys');
    const hasApiConfig = columns.some(col => col.Field === 'api_config');
    
    if (!hasApiConfig) {
      await sequelize.query('ALTER TABLE api_keys ADD COLUMN api_config JSON NULL COMMENT "API扩展配置信息"');
      console.log('✅ api_config字段添加成功');
    } else {
      console.log('ℹ️ api_config字段已存在');
    }
    
    // 验证字段添加结果
    const [newColumns] = await sequelize.query('DESCRIBE api_keys');
    const apiConfigField = newColumns.find(col => col.Field === 'api_config');
    if (apiConfigField) {
      console.log('📊 api_config字段信息:', apiConfigField);
    }
    
    console.log('\n🎯 更新后的表结构:');
    newColumns.forEach(col => {
      console.log(`${col.Field}: ${col.Type} ${col.Null === 'YES' ? 'NULL' : 'NOT NULL'}`);
    });
    
    process.exit(0);
  } catch (error) {
    console.error('❌ 添加字段失败:', error);
    process.exit(1);
  }
}

addApiConfigField();